package com.maimao.core.model.resp;

import com.maimao.core.enums.AuthEnum;
import com.maimao.core.enums.ExceptionEnum;
import com.maimao.core.enums.RespCode;

import java.io.Serializable;

/**
 * @author MaoLin Wang
 * @date 2020/11/27 10:08 下午
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 8961788437022562205L;

    private int code;
    private T data;
    private boolean flag;
    private String message;

    public Result(AuthEnum authEnum, boolean flag) {
        this.code=authEnum.getCode();
        this.flag=flag;
        this.message=authEnum.getMessage();
    }

    public Result(ExceptionEnum em, boolean flag) {
        this.code=em.getCode();
        this.flag=flag;
        this.message=em.getMessage();
    }

    public static Result ok (){
        return new Result(RespCode.OK.getCode(),true,RespCode.OK.getMsg());
    }

    public static Result fail(){
        return new Result(RespCode.FAIL.getCode(),false,RespCode.FAIL.getMsg());
    }

    public static Result ok(Object data, String msg){
        return new Result(RespCode.OK.getCode(),data,true,msg);
    }
    public static Result ok(String msg){
        return new Result(RespCode.OK.getCode(),null,true,msg);
    }
    public static Result ok(RespCode respCode){
        return new Result(respCode.getCode(),null,true,respCode.getMsg());
    }
    public static Result ok(RespCode respCode, String message){
        return new Result(respCode.getCode(),null,true,message);
    }
    public static Result ok(RespCode respCode, String message, Object data){
        return new Result(respCode.getCode(),data,true,message);
    }
    public static Result okData(RespCode respCode, Object data){
        return new Result(respCode.getCode(),data,true,respCode.getMsg());
    }
    public static <T> Result okData(T data){
        return new Result<T>(200, data, true, "ok");
    }
    public static Result fail(Integer code, String msg){
        return new Result(code,null,false,msg);
    }
    public static Result fail(Object data, String msg){
        return new Result(RespCode.FAIL.getCode(),data,false,msg);
    }
    public static Result fail(String msg){
        return new Result(RespCode.FAIL.getCode(),null,false,msg);
    }
    public static Result fail(RespCode respCode){
        return new Result(respCode.getCode(),null,false,respCode.getMsg());
    }
    public static Result fail(RespCode respCode, String message){
        return new Result(respCode.getCode(),null,false,message);
    }
    public static Result notValid(String message){
        return new Result(RespCode.NOT_VALID.getCode(),null,false,message);
    }
    public static Result notValid(){
        return new Result(RespCode.NOT_VALID.getCode(),null,false,RespCode.NOT_VALID.getMsg());
    }
    public static Result fail(RespCode respCode, Object data){
        return new Result(respCode.getCode(),data,false,respCode.getMsg());
    }


    public Result() {
    }

    public Result(int code, T data, boolean flag, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.flag = flag;
    }

    public Result(int code, boolean flag, String message) {
        this.code = code;
        this.flag = flag;
        this.message = message;
    }

    public static Result fail(AuthEnum em, boolean flag) {
        return new Result(em,flag);
    }

    public static Result fail(ExceptionEnum em, boolean flag) {
        return new Result(em,flag);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "ResResult{" +
                "code=" + code +
                ", data=" + data +
                ", flag=" + flag +
                ", message='" + message + '\'' +
                '}';
    }
}
